<?php
namespace Admin\Controller;
class MenuController extends CheckController{

    //分配Article模板页面
    function index(){
        $catid=isset($_GET['id'])?I('get.id'):0;
        $this->page_title="  تىزىملىك باشقۇرۇش";
        $where_catid="pid=0";
        if($catid){
            $where_catid.=" and catid=".$catid;
        }
        $menu=_sort(M("menu")->where($where_catid)->order("sort asc")->select());
        //所有的粉类
        $classify=M("linkcate")->select();
        $this->assign("classify",$classify);
        $this->assign("get_id",$catid);
        $this->assign('menu',$menu);
        $this->display("index");
    }
    //分配页面
    public function add(){
        $this->page_title="تىزىملىك قوشۇش";
        $menu=_sort(M("menu")->select());
        $this->assign('menu',$menu);
        //qong tur
        $linkcate=_sort(M("linkcate")->select());
        $this->assign("linkcate",$linkcate);
        //barlik ana tizimliklar
        $classify=_sort(M("menu")->select());
        $this->assign("classify",$classify);
        $this->display("add");
    }
    //接收数据并且写入到数据库
    public function addhend(){
        //pax, qikit, yanto siziki , boxluk bilan kuep tizimlik koxuxni amalga axuridu
        if(stripos(trim(I('post.name')),"،")){
            $post_data['names']=explode("،",I('post.name'));
        }elseif (stripos(trim(I('post.name')),",")){
            $post_data['names']=explode(",",I('post.name'));
        }elseif (stripos(trim(I('post.name')),"  ")){
            $post_data['names']=explode("  ",I('post.name'));
        }elseif(stripos(trim(I('post.name'))," ")){
            $post_data['names']=I('post.name');
        }elseif(is_numeric(trim(I('post.name')))){
            $post_data['names']=I('post.name');
        }
        $time=date("Y-m-d H:i");
        $rules = array(
            array('catid','require','چوڭ تۈر تاللاڭ',1),
            array('pid','require','ماس سەھىپە تاللاڭ',2),
            array('name','require','تىزىملىك نامىنى چۇقۇم يېزىڭ！',1) //默认情况下用正则进行验证
        );
        if(!M("Menu")->validate($rules)->create()){
            $this->error(M("Menu")->getError());
        }else{
            $pid=I('post.pid');
            //agar kuep  tizimlik koxsa bu ejra bolidu
            $i=M("Menu")->where("pid=$pid")->max('sort');
            if(is_array($post_data['names'])){
                foreach ($post_data['names'] as $name){
                    $name=htmlspecialchars(trim($name));
                    $i++;
                    $data=array(
                        'name'=>$name,
                        'sort'=>I('post.sort')+$i,
                        'catid'=>I('post.catid'),
                        'pid'=>I('post.pid'),
                        'status'=>I('post.status'),
                        'intime'=>$time,
                        'uptime'=>$time
                    );
                    $result=M("Menu")->add($data);
                }
            }else{
                //agar bit tal ayrim tizimlik koxsa bu ejra bolidu
                $i=M("Menu")->where("pid=$pid")->max('sort');
                $i++;
                $post=I('post.');
                $post['sort']=I('post.sort')+$i;
                $post['intime']=$time;
                $post['uptime']=$time;
                $result=M("Menu")->add($post);
            }
            if($result){
                $this->display("../index");
                _check_alert("مۇۋاپىقىيەتلىك بولدى !","index.html",1);
            }
        }
    }
    //编辑分类
    public function edit(){
        $id=I('get.id');
        $this->page_title="تىزىملىك تەھرىرلەش";
        //获取要编辑的一条数据
        $classifys=M("menu")->find($id);
        $this->assign("classifys",$classifys);
        //qong tur
        $linkcate=_sort(M("linkcate")->select());
        $this->assign("linkcate",$linkcate);
        //barlik ana tizimliklar
        $classify=_sort(M("menu")->select());
        $this->assign("classify",$classify);
        $this->display("edit");
    }
    //接收数据并且保存到数据库
    public function savehend(){
        $data['id']=I('post.id');
        $rules = array(
            array('catid','require','  چوڭ تۈرنى تاللاڭ!',1),
            array('name','require','  ئېلان ئىسمىنى يېزىڭ!',1),
        );
        $User=M("Menu");
        $post=I('post.');
        $post['icon']=$_POST['icon']?$post['icon']:"no";
        if(!$User->validate($rules)->create()){
            $this->error($User->geterror(),"",1);
        }else{
            $result =$User->save($post); // 保存到数据库
            if($result!==false){
                $this->display("../index");
                _check_alert(" ئۆزگەرتىش مۇۋاپىقيەتلىك بولدى !","index.html",1);
            }
        }
    }
    //用ajax删除分类
    public function del(){
        $id=I('post.id');
        $ret = M("Menu")->where(array('pid'=>$id))->getField('id',true);
        //如果有子分传给deleteallclass( )函数
        if(is_array($ret)){
            $this->deleteallclass($ret);
        }
        //获取要编辑的一条数据
        $data=M("Menu")->delete($id);
        if(!$data){
            $data=array("status"=>0,"message"=>"ئۆچۈرۈش مەغلۇب بولدى ، قايتا سىناڭ !");
        }else{
            $data=array("status"=>1,"message"=>"ئۆچۈرۈش مۇۋاپىقيەتلىك بولدى !");
        };
        $this->ajaxReturn($data);
    }
    //如果有子分类删除
    private function deleteallclass($ids){
        $mod = M('Menu');
        $ret2=implode(',',$ids);
        $mod->delete($ret2);
    }

//用ajax添加url
    public function url(){
        $post=array();
        $id=I('post.id');
        $post['url']=I('post.url');
        //获取要编辑的一条数据
        $data=M("Menu")->where("id='$id'")->save($post);
        if(!$data){
            $data=array("status"=>0,"message"=>"ئۇلانما قوشۇش مەغلۇب بولدى ، قايتا سىناڭ !");
        }else{
            $data=array("status"=>1,"message"=>"ئۇلانما قوشۇش مۇۋاپىقيەتلىك بولدى !");
        };
        $this->ajaxReturn($data);
    }
    //用ajax排序分类
    public function sortfen(){
        $post=array();
        $id=I('post.id');
        $post['sort']=I('post.sort');
        //获取要编辑的一条数据
        $data=M("Menu")->where("id='$id'")->save($post);
        if(!$data){
            $data=array("status"=>0,"message"=>"رەتلەش مەغلۇب بولدى ، قايتا سىناڭ !");
        }else{
            $data=array("status"=>1,"message"=>"رەتلەش مۇۋاپىقيەتلىك بولدى !");
        };
        $this->ajaxReturn($data);
    }

    //用ajax分类状态
    public function sortStatus(){
        $id=I('post.id');
        $post['status']=I('post.status');
        if(!M('Menu')->where("id='$id'")->save($post)){
            $data=array("status"=>0,"content"=>"مەغلۇب بولدى ، قايتا سىناڭ !");
        }else{
            $data=array("status"=>1,"content"=>"مۇۋاپقىيەتلىك بولدى !");
        }
        $this->ajaxReturn($data);
    }

    public function icon(){
        if(IS_AJAX){
            $id=I('post.id');
            $post['icon']=I('post.thumb');
            $res=M("menu")->where("id='$id'")->save($post);
            if($res){
                $data=array("status"=>1,"message"=>"مۇۋاپىقىيەتلىك بولدى");
            }else{
                $data=array("status"=>0,"message"=>"مەغلۇب بولدى");
            }
            $this->ajaxReturn($data);
        }else{
            $this->display();
        }
    }


}